package com.youcan.controller.modelcontroller;


import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.youcan.entity.User;
//import com.youcan.search.helper.SolrHelper;
import com.youcan.service.modelservices.UserService;
import com.youcan.util.Constants;
/**
 * @author LiChao E-mail:what_is_javascript@163.com
 * @date:2012-8-17 下午11:07:06
 * @version :V1.0
 *
 */
//@Component //组件标签，扫描器可自动扫到
@Controller //控制器标签，扫描控制器时装载此bean 必须
@RequestMapping("/user")//实现rest 根据业务类型进行下面的url进行操作，如用户注册http://www.youcan.com/user/register
public class UserController{

	protected final Log logger = LogFactory.getLog(UserController.class);

	@Autowired//自动注入，有此可以不用写setter、getter
	@Qualifier("userServiceImpl")//指定注入接口的具体实现类
	private UserService userService;

	@RequestMapping(value = "/userreg", method = RequestMethod.GET)//指定某类型请求所访问的具体方法
	public String sendToRegist() {
//		System.out.println("UserController - returning hello view ");
		return "user/register";
	}
	

	//通过注解进行转发控制 POST来的请求并把参数绑定,@ResponseBody解释本方法以字符流传到view
	/*@ResponseBody  
	@RequestMapping(value = "/regist", method = RequestMethod.POST)
	public String create(@RequestParam("mail") String mail,
			@RequestParam("userName") String userName,
			@RequestParam("password") String password,
			HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		User user = new User();
//		JSONObject json = new JSONObject();
		
		try {
			user.setMail(mail);
			user.setUserName(userName);
			user.setPwd(password);
			System.out.println("-------------");
			userService.addUser(user);
			return "注册成功。";
		} catch (Exception re) {
			return "您失败了，不要凌乱，失败是下次成功的基石";
//			return new ModelAndView("failed");
		}
//		return json;

//		return new ModelAndView("success");
	}*/
	
	/*@ResponseBody 
	@RequestMapping(value = "/regist", method = RequestMethod.POST)
	public JSONObject create(@RequestParam("mail") String mail,
			@RequestParam("userName") String userName,
			@RequestParam("password") String password,
			HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException, JSONException {
		
		List<User> userList = new ArrayList<User>();
		User user = new User();
//		Collect collect = new Collect();
//		collect.setId(2);
//		collect.setGoodsId("测试1");
		
		JSONObject json = new JSONObject();
		
		try {
			user.setMail(mail);
			user.setUserName(userName);
			user.setPwd(password);
			userList.add(user);
			userService.addEntity(user);
			json.put(Constants.RES_CODE, Constants.CODE_SUCCESS);
//			json.put("user", user);
			json.put(Constants.RES_MESSAGE, "注册成功。");
		} catch (Exception re) {
			json.put(Constants.RES_CODE, Constants.CODE_FAILED);
			json.put(Constants.RES_MESSAGE, "注册失败。");
		}
		return json;

	}*/
	
	
	
	@ResponseBody //把返回值直接写到HTTP response body
	@RequestMapping(value = "/regist", method = RequestMethod.POST)
	public JSONObject create(@ModelAttribute User user) throws JSONException{
		
		JSONObject json = new JSONObject();
		
		try {
			System.out.println("邮件地址："+user.getMail());
//			SolrHelper solrHelper = new SolrHelper("core1");
			userService.addEntity(user);
			json.put(Constants.RES_CODE, Constants.CODE_SUCCESS);
//			json.put("user", user);
			json.put(Constants.RES_MESSAGE, "注册成功。");
		} catch (Exception re) {
			json.put(Constants.RES_CODE, Constants.CODE_FAILED);
			json.put(Constants.RES_MESSAGE, "注册失败。");
		}
		return json;

	}
	
	
	@RequestMapping(value = "/users", method = RequestMethod.GET)
	public ModelAndView getUserList(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
//		JSONObject json = new JSONObject();
		
//		 	List<User> userList = userService.findAll("User");
		 	List<User> userList = userService.findAll("");
//		 	List<User> userList = new ArrayList<User>();
//		 	userList.add(userService.findById(1));
//			System.out.println("USERS-------------");
//			System.out.println("Size------------"+userList.size());
			
			for(User u:userList){
				System.out.println("Email:"+u.getMail());
			}
			ModelAndView mav = new ModelAndView("user/userlist");
			
			mav.addObject(userList);
			
			return mav;
//		return json;

//		return new ModelAndView("success");
	}
	
}
